package br.unifor.cct.mia.initialize;

import java.util.List;

import br.unifor.cct.mia.dataenhancement.Attribute;
import br.unifor.cct.mia.dataenhancement.Structure;
import br.unifor.cct.mia.evolutionary.FeatureConstructionGenotype;
import br.unifor.cct.mia.ga.Ga;
import br.unifor.cct.mia.ga.util.FeatureConstructionGene;
import br.unifor.cct.mia.ga.util.FeatureConstructionOperators;
import br.unifor.cct.mia.util.Methods;

public class RepeatableFeatureConstructionInitialize implements Initialize {

	public Object execute(Object value) {	
		Object[] o = (Object[]) value;
		Structure struc = (Structure) o[0];
		Ga ga = (Ga)o[1];
		Integer specieCount = (Integer)o[2];
				
		ga.population = new FeatureConstructionGenotype[ga.configurations.getPopsize()+1];
        
        for (int i = 0; i < ga.configurations.getPopsize(); i++) {
        	ga.population[i] = new FeatureConstructionGenotype(ga.configurations);
        	
        	ga.population[i].setFitness(0);
        	ga.population[i].setSelectProbability(0);      
        	ga.population[i].setSpeciesCount(specieCount.intValue());           
            
        	FeatureConstructionGene genes[] = new FeatureConstructionGene[ga.configurations.getNvars()];
            for (int j = 0; j < ga.configurations.getNvars(); j++) { 
            	int dbSize = struc.size();
            	
            	int attNum = Methods.randIntValue(0,dbSize-1);
            	Attribute atributo = struc.getAttribute(attNum);
            	List values = struc.getValues(attNum);
            	int vlNum = Methods.randIntValue(0,values.size()-1);
            	
            	FeatureConstructionGene gene = new FeatureConstructionGene();
            	gene.setAttribute( atributo.getName() );
            	gene.setAttPosition(attNum);
            	gene.setValue( String.valueOf( values.toArray()[vlNum] ) );
            	gene.setActiveBit((byte)Methods.randIntValue(0,1));
            	if ( atributo.getTipo().equalsIgnoreCase(Attribute.DISCRETE) ) {
            		gene.setOperator( FeatureConstructionOperators.IGUAL );
            	}
            	else {
            		gene.setOperator( FeatureConstructionOperators.getRandom() );
            	}
            	genes[j] = gene;
            }
            Methods.verificarMinActive(genes);
            ((FeatureConstructionGenotype)ga.population[i]).setGene(genes);            
        }
	        
		return ga.population;
	}

}
